package org.testmyapp.engine;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import org.activiti.cdi.CdiJtaProcessEngineConfiguration;
import org.activiti.cdi.spi.ProcessEngineLookup;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;

@Named
@SessionScoped
public class EngineProvider implements Serializable,ProcessEngineLookup {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8723992524857786197L;
	
	private ProcessEngine engine = null;
	private static final String dataSourceJndiName = "jdbc/MyPostgres";
	private static final String history = "full";

	public ProcessEngine getEngine() {
		
		if (engine == null) {
			ProcessEngines.init();
			CdiJtaProcessEngineConfiguration cdiEngineConf = new CdiJtaProcessEngineConfiguration();
			cdiEngineConf.setDataSourceJndiName(dataSourceJndiName);
			cdiEngineConf.setHistory(history);
			ProcessEngineConfiguration engConf = ProcessEngineConfiguration
					.createStandaloneProcessEngineConfiguration();
			engConf.setDataSourceJndiName(dataSourceJndiName);
			engConf.setHistory(history);
			engine = cdiEngineConf.buildProcessEngine();
		}
		return engine;
	}

	public int getPrecedence() {
		// TODO Auto-generated method stub
		return 0;
	}

	public ProcessEngine getProcessEngine() {
		return getEngine();
	}

	public void ungetProcessEngine() {
		// TODO Auto-generated method stub
		
	}
	
}
